// index.js
import api from "../../utils/api"
Page({
    data: {
        dataDict: {
            data: [
                {
                    "id": 45,
                    "name": "牛牛",
                    "area": "#19",
                    "avatar": "http://192.168.1.100:8001/media/bank/2023/04/02/79sq2lK9rKlP92fdcdb83e3bbaf06a79821058a09349.jpg"
                },
            ],
            today_count: 0,
            total_count: 0,
        }
    },
    // 跳转到信息采集页面
    bindToForm: function () {
        wx.navigateTo({
            url: '/pages/formAuth/formAuth'
        });
    },

    // 跳转到数据统计页面
    bindToStatistics: function () {
        wx.navigateTo({
            url: '/pages/statistics/statistics'
        });
    },
    addRow(row) {
        var dataList = this.data.dataDict.data
        dataList.unshift(row)
        this.setData({
            ["dataDict.data"]: dataList,
            ["dataDict.total_count"]: this.data.dataDict.total_count + 1,
            ["dataDict.today_count"]: this.data.dataDict.today_count + 1
        })
    },

    // 删除记录
    deleteRow: function (e) {
        wx.showModal({
            title: '确认是否删除',
            confirmColor: "#ff461f",
            success: (res) => {
                if (!res.confirm) {
                    return
                }
                const nid = e.currentTarget.dataset.nid;
                const index = e.currentTarget.dataset.index;
                let dataList = this.data.dataDict.data;

                dataList.splice(index, 1); // 从数据中删除该记录
                wx.showLoading({
                    title: '删除中',
                    mask: true
                })
                
                wx.request({
                    url: api.bank + nid + '/',
                    method: 'DELETE',
                    success: (res) => {
                        let total_count = this.data.dataDict.total_count - 1
                        if (total_count < 0) {
                            total_count = 0
                        }
                        let today_count = this.data.dataDict.today_count - 1
                        if (today_count < 0) {
                            today_count = 0
                        }
                        this.setData({
                            ["dataDict.data"]: dataList,
                            ["dataDict.total_count"]: total_count,
                            ["dataDict.today_count"]: today_count
                        })
                    },
                    complete: function () {
                        wx.hideLoading()
                    }
                })
            }
        })
    },
    // deleteRow: function (e) {
    //     const nid = e.currentTarget.dataset.nid;
    //     const index = e.currentTarget.dataset.index;
    //     wx.showModal({
    //         title: '确认是否删除',
    //         confirmColor: "#ff461f",
    //         success: (res) => {
    //             if (res.confirm) {
    //                 this.performDelete(nid, index);
    //             }
    //         }
    //     });
    // },
    
    // performDelete: function (nid, index) {
    //     let dataList = this.data.dataDict.data;
    
    //     // 提前从数据中删除该记录
    //     dataList.splice(index, 1);
    
    //     wx.showLoading({
    //         title: '删除中',
    //         mask: true
    //     });
    
    //     wx.request({
    //         url: api.bank + nid + '/',
    //         method: 'DELETE',
    //         success: (res) => {
    //             let total_count = this.data.dataDict.total_count - 1;
    //             if (total_count < 0) {
    //                 total_count = 0;
    //             }
    //             let today_count = this.data.dataDict.today_count - 1;
    //             if (today_count < 0) {
    //                 today_count = 0;
    //             }
    //             this.setData({
    //                 ["dataDict.data"]: dataList,
    //                 ["dataDict.total_count"]: total_count,
    //                 ["dataDict.today_count"]: today_count
    //             });
    //         },
    //         complete: function () {
    //             wx.hideLoading();
    //         }
    //     });
    // },
    
    refresh() {
        // 1.发送网络请求
        // 2.数据绑定
        wx.showLoading({ mask: true })
        wx.request({
            url: api.bank,  //http://192.168.1.100:8001/api/bank/
            method: "GET",
            success: (res) => {
                this.setData({
                    dataDict: res.data
                })
            },
            complete() {
                wx.hideLoading()
            },
        })
    },
    /**
         * 生命周期函数--监听页面加载
         */
    onLoad(options) {
        this.refresh()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log("加入骑士监听用户下拉动作")
        this.refresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})